﻿package com.tools
{
	import flash.net.*;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.ui.ContextMenuBuiltInItems;
    import flash.events.ContextMenuEvent;
	import flash.display.Sprite;
	
	import com.view.PlayerView;
	import flash.display.Stage;
	
	import com.conf.PlayerSpec;
	
	public class PlayerContextMenu extends Sprite
	{
		private var _availableSkins	: Array = PlayerSpec.AVAILABLE_SKINS;
		private var myContextMenu	: ContextMenu;	
		private var _stage			: Stage;
		private var _view			: PlayerView;
		
		function PlayerContextMenu ( cible : PlayerView, stageCurrent : Stage )
		{
			_stage = stageCurrent;
			
			myContextMenu = new ContextMenu();
			removeDefaultItems();
			addCustomMenuItems();
			myContextMenu.addEventListener( ContextMenuEvent.MENU_SELECT, menuSelectHandler );
			
			//j'attache le contexteMenu au clip passé en paramètre, dans l'exemple la racine
			_view = cible;
			_view.contextMenu = myContextMenu;
		}
		
		private function removeDefaultItems():void {
			//suppression des items par défaut
			myContextMenu.hideBuiltInItems();
		}
		
		private function addCustomMenuItems():void {
			//ajout des items personnalisé et des évènements associés
			//le copyright
			var lng 	: uint 	= _availableSkins.length;
			var item	: ContextMenuItem;
			
			for ( var i = 0; i < lng ; i ++)
			{
				item = new ContextMenuItem( _availableSkins[i].name );
				myContextMenu.customItems.push( item );			
				item.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, menuSelectHandler );
			}
			
		}
		
		private function menuSelectHandler( pEvt : ContextMenuEvent ) : void
		{
			var lng 	: uint 	= _availableSkins.length;
			
			for ( var i : uint = 0 ; i < lng ; i ++ )
			{
				if ( pEvt.currentTarget is ContextMenuItem )
				{
					switch( pEvt.currentTarget.caption )
					{
						case _availableSkins[i].name:
						_view.initPlayerView( false, _availableSkins[i].location );
						handleMenuState( i );
						break;
						
						default:
						break;
					}
				}
			}
			/*
			event.target.customItems[2].enabled = true; // show full screen button
			event.target.customItems[3].enabled = false; // hide normal screen button
			*/
		}
		
		public function handleMenuState( pIndex : uint ) : void
		{
			var lng : uint = myContextMenu.customItems.length;
			for ( var i : uint = 0 ; i < lng ; i ++ )
			{
				if ( i == pIndex )
					myContextMenu.customItems[i].enabled = false;
				else
					myContextMenu.customItems[i].enabled = true;
			}
		}
	}
}